package com.wcpdoc.exam.api.controller;


import com.wcpdoc.core.controller.BaseController;
import com.wcpdoc.core.entity.PageIn;
import com.wcpdoc.core.entity.PageResult;
import com.wcpdoc.core.entity.PageResultEx;
import com.wcpdoc.core.exception.MyException;
import com.wcpdoc.core.util.ValidateUtil;
import com.wcpdoc.exam.core.service.ResourceService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Classname ResourceController
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/4/5 14:57
 * @Created by CALVIN
 */
@RestController
@RequestMapping("api/resource")
public class ApiResourceController extends BaseController {

    @Resource
    ResourceService resourceService;

    @PostMapping("createDir")
    public PageResult createDir(String name,Integer id){
        return resourceService.createDir(name,id,getCurUser().getId());
    }

    @PostMapping(value = "/upload", headers = "content-type=multipart/form-data")
    public PageResult upload(@RequestParam(value = "parentId", required = true) Integer parentId,
                          @RequestParam("file") MultipartFile file){
        return resourceService.upload(file,parentId,getCurUser().getId());
    }

    @PostMapping("delete")
    public PageResult delete(Integer id){
        return resourceService.delete(id,getCurUser().getId());
    }

    /**
     * 修改数据库文件名，未修改真实的文件名
     *
     * @return PageResult
     */
    @RequestMapping("/edit")
    @ResponseBody
    public PageResult edit(Integer id,String name) {
        try {
            // 校验数据有效性
            if (!ValidateUtil.isValid(name)) {
                throw new MyException("参数错误：name");
            }
            // 修改机构
            com.wcpdoc.exam.core.entity.Resource entity = resourceService.getEntity(id);
            entity.setResourceName(name);
            entity.setUpdateTime(new Date());
            entity.setUpdateUserId(getCurUser().getId());
            resourceService.update(entity);
            return PageResult.ok();
        } catch (MyException e) {
            return PageResult.err().msg(e.getMessage());
        } catch (Exception e) {
            return PageResult.err();
        }
    }

    /**
     * 获取机构
     *
     * @param id
     * @return PageResult
     */
    @RequestMapping("/get")
    @ResponseBody
    public PageResult get(Integer id) {
        try {
            com.wcpdoc.exam.core.entity.Resource entity = resourceService.getEntity(id);
            return PageResultEx.ok()
                    .addAttr("id", entity.getId())
                    .addAttr("name", entity.getResourceName());
        } catch (MyException e) {
            return PageResult.err().msg(e.getMessage());
        } catch (Exception e) {
            return PageResult.err();
        }
    }

    @PostMapping("listResource")
    public PageResult listResource(String search,Integer parentId){
        PageIn pageIn = new PageIn(request);
        if(ValidateUtil.isValid(search)){
            pageIn.addAttr("search", search);
        } if(ValidateUtil.isValid(parentId)){
            pageIn.addAttr("parentId", parentId);
        }
        return resourceService.listResource(pageIn);
    }

    @PostMapping(value = "/uploadAvatar", headers = "content-type=multipart/form-data")
    public PageResult upload(@RequestParam("file") MultipartFile file){
        return resourceService.uploadAvatar(file,getCurUser().getId());
    }

}
